#提取数据库中的图片并保存在 jm文件夹中
import pymysql
from urllib import request
import os
# 创建的目录
path = "./data/jm/"
if not os.path.exists(path):
    os.makedirs(path)

mydb = pymysql.connect(
  host="8.130.54.155",
  user="cyw",
  passwd="123456",
  database="QingMiao"
)

mycursor = mydb.cursor()

#mycursor.execute("SELECT bareheaded_photo FROM student")
mycursor.execute("SELECT bareheaded_photo FROM student limit 5") #测试时使用这个语句

myresult = mycursor.fetchall()

back = [x[0] for x in myresult]

# 使用列表推导式和字符串格式化来生成最终的URL
front = "https://seedling-classmate.oss-cn-beijing.aliyuncs.com/"
urls = [front + x for x in back]

# 打印生成的URL列表
for url in urls:
    #print(url)
    filename = url.split('-')[4]
    # filename=url.split('id-')[1]
    # print(filename)
    request.urlretrieve(url, './data/jm/' + filename + '.jpg')



